package gat.common.util;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;

/**
 * @Author：liucaihong
 * @date：2023/4/19 13:49
 * @description:
 */
public class Aes {
    private String ALGO = "AES";
    private byte[] iv; //截取 securityKey的前16位
    private String securityKey = "";
    private String method = "AES/CBC/PKCS5Padding";

    public Aes(String method, String securityKey, String iv) throws UnsupportedEncodingException {
        this.securityKey = securityKey;
        this.method = method;
        this.iv = this.initIV(iv);
    }

    /**
     * run test
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        String method = "AES/CBC/PKCS5Padding";
        String securityKey16 = "987654321123456789987654";
        String iv = "9876543211234567";  //截取 securityKey的前16位
        String plainText = "4506d3267c7339ea26ad4cdb193d84bb";
        Aes aes = new Aes(method, securityKey16, iv);
        System.out.println("原始---"+ plainText);
        String cipherText = aes.encrypt(plainText);
        System.out.println("加密---:"+cipherText);
    }

    /**
     * encrypt
     * @param plainText
     * @return
     * @throws Exception
     */
    public String encrypt(String plainText) throws Exception {
        try {
            Cipher cipher = Cipher.getInstance(this.method);
            byte[] dataBytes = plainText.getBytes("UTF-8");
            SecretKeySpec keyspec = new SecretKeySpec(this.securityKey.getBytes("UTF-8"), ALGO);
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(dataBytes);
            String EncStr = Base64Util.encode(encrypted);
            return EncStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * decrypt
     * @param cipherText
     * @return
     * @throws Exception
     */
    public String decrypt(String cipherText) throws Exception {
        try {
            byte[] encrypted1 = Base64.decodeBase64(cipherText);
            Cipher cipher = Cipher.getInstance(this.method);
            SecretKeySpec keyspec = new SecretKeySpec(this.securityKey.getBytes("UTF-8"), ALGO);
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "UTF-8");
            return originalString.trim();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * initIV
     */
    private byte[] initIV(String IVString) {
        try {
            return IVString.getBytes("UTF-8");
        } catch (Exception e) {
            int blockSize = 16;
            byte[] iv = new byte[blockSize];
            for (int i = 0; i < blockSize; ++i) {
                iv[i] = 0;
            }
            return iv;
        }
    }
}
